package cn.tedu.duotai;

public class PersonDemo {
    public static void main(String[] args) {
        //创建Person对象
//        Person p = new Person();
//        p.name = "张三";
//        p.age = 28;
//        p.eat();
        //创建Teacher对象
//        Teacher teacher = new Teacher();
//        teacher.name = "张老师";
//        teacher.age = 30;
//        teacher.type = "数学";
//        teacher.eat();
//        teacher.teach();

        //向上造型--自动类型转换
        Person p = new Teacher();
        p.name = "张老师";
        p.age = 30;
        //p.type = "数学";//编译报错 编译看左边 p对象时Person类型 Person中没有type属性 编译报错
        p.eat();
        //向下造型--将父类类型的引用强转为子类类型 可以调用子类特有的属性和方法
        //强制类型的转换
        Teacher t1 = (Teacher)p;//Person->Teacher
        t1.type = "数学";
        t1.teach();

        Person p1 = new Student();
        System.out.println(p1);
        p1.name = "张同学";
        p1.eat();
        //向下造型
        Student s1 = (Student) p1;
        System.out.println(s1);
        s1.study();
        System.out.println("==========");
        Teacher t2 = (Teacher) p1;//编译不报错
        t2.teach();

        //Pet pet = (Pet)p1;//编译会报错


    }
}
